home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2002 November / CD 1 / APC0211D1.ISO / workshop / prog / files / ActivePerl-5.6.1.633-MSWin32.msi / _faff0a30dd9c0da983411e3b1c4a2f38 < prev    next >
Encoding:
Text File  |  2001-12-28  |  2.3 KB  |  100 lines

  1. package Thread::Queue;
  2. use Thread qw(cond_wait cond_broadcast);
  3.  
  4. =head1 NAME
  5.  
  6. Thread::Queue - thread-safe queues
  7.  
  8. =head1 SUPPORTED PLATFORMS
  9.  
  10. none
  11.  
  12. =head1 SYNOPSIS
  13.  
  14.     use Thread::Queue;
  15.     my $q = new Thread::Queue;
  16.     $q->enqueue("foo", "bar");
  17.     my $foo = $q->dequeue;    # The "bar" is still in the queue.
  18.     my $foo = $q->dequeue_nb; # returns "bar", or undef if the queue was
  19.                               # empty
  20.     my $left = $q->pending;   # returns the number of items still in the queue
  21.  
  22. =head1 DESCRIPTION
  23.  
  24. A queue, as implemented by C<Thread::Queue> is a thread-safe data structure
  25. much like a list. Any number of threads can safely add elements to the end
  26. of the list, or remove elements from the head of the list. (Queues don't
  27. permit adding or removing elements from the middle of the list)
  28.  
  29. =head1 FUNCTIONS AND METHODS
  30.  
  31. =over 8
  32.  
  33. =item new
  34.  
  35. The C<new> function creates a new empty queue.
  36.  
  37. =item enqueue LIST
  38.  
  39. The C<enqueue> method adds a list of scalars on to the end of the queue.
  40. The queue will grow as needed to accomodate the list.
  41.  
  42. =item dequeue
  43.  
  44. The C<dequeue> method removes a scalar from the head of the queue and
  45. returns it. If the queue is currently empty, C<dequeue> will block the
  46. thread until another thread C<enqueue>s a scalar.
  47.  
  48. =item dequeue_nb
  49.  
  50. The C<dequeue_nb> method, like the C<dequeue> method, removes a scalar from
  51. the head of the queue and returns it. Unlike C<dequeue>, though,
  52. C<dequeue_nb> won't block if the queue is empty, instead returning
  53. C<undef>.
  54.  
  55. =item pending
  56.  
  57. The C<pending> method returns the number of items still in the queue.  (If
  58. there can be multiple readers on the queue it's best to lock the queue
  59. before checking to make sure that it stays in a consistent state)
  60.  
  61. =back
  62.  
  63. =head1 SEE ALSO
  64.  
  65. L<Thread>
  66.   
  67. =cut
  68.  
  69. sub new {
  70.     my $class = shift;
  71.     return bless [@_], $class;
  72. }
  73.  
  74. sub dequeue : locked : method {
  75.     my $q = shift;
  76.     cond_wait $q until @$q;
  77.     return shift @$q;
  78. }
  79.  
  80. sub dequeue_nb : locked : method {
  81.   my $q = shift;
  82.   if (@$q) {
  83.     return shift @$q;
  84.   } else {
  85.     return undef;
  86.   }
  87. }
  88.  
  89. sub enqueue : locked : method {
  90.     my $q = shift;
  91.     push(@$q, @_) and cond_broadcast $q;
  92. }
  93.  
  94. sub pending : locked : method {
  95.   my $q = shift;
  96.   return scalar(@$q);
  97. }
  98.  
  99. 1;
  100.